home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libiconv_src.lha / tests / table-from.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-07  |  4.2 KB  |  144 lines

  1. /* Copyright (C) 2000 Free Software Foundation, Inc.
  2.    This file is part of the GNU ICONV Library.
  3.  
  4.    The GNU ICONV Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.  
  9.    The GNU ICONV Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU ICONV Library; see the file COPYING.LIB.  If not,
  16.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.    Boston, MA 02111-1307, USA.  */
  18.  
  19. /* Create a table from CHARSET to Unicode. */
  20.  
  21. #include <stddef.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <iconv.h>
  25. #include <errno.h>
  26.  
  27. static const char* hexbuf (unsigned char buf[], unsigned int buflen)
  28. {
  29.   static char msg[50];
  30.   switch (buflen) {
  31.     case 1: sprintf(msg,"0x%02X",buf[0]); break;
  32.     case 2: sprintf(msg,"0x%02X%02X",buf[0],buf[1]); break;
  33.     case 3: sprintf(msg,"0x%02X%02X%02X",buf[0],buf[1],buf[2]); break;
  34.     case 4: sprintf(msg,"0x%02X%02X%02X%02X",buf[0],buf[1],buf[2],buf[3]); break;
  35.     default: abort();
  36.   }
  37.   return msg;
  38. }
  39.  
  40. static int try (iconv_t cd, unsigned char buf[], unsigned int buflen, unsigned int *out)
  41. {
  42.   const char* inbuf = (const char*) buf;
  43.   size_t inbytesleft = buflen;
  44.   char* outbuf = (char*) out;
  45.   size_t outbytesleft = sizeof(unsigned int);
  46.   size_t result = iconv(cd,&inbuf,&inbytesleft,&outbuf,&outbytesleft);
  47.   if (result == (size_t)(-1)) {
  48.     if (errno == EILSEQ) {
  49.       return -1;
  50.     } else if (errno == EINVAL) {
  51.       return 0;
  52.     } else {
  53.       int saved_errno = errno;
  54.       fprintf(stderr,"%s: iconv error: ",hexbuf(buf,buflen));
  55.       errno = saved_errno;
  56.       perror("");
  57.       exit(1);
  58.     }
  59.   } else if (result > 0) /* ignore conversions with transliteration */ {
  60.     return -1;
  61.   } else {
  62.     if (inbytesleft != 0 || outbytesleft != 0) {
  63.       fprintf(stderr,"%s: inbytes = %ld, outbytes = %ld\n",hexbuf(buf,buflen),(long)(buflen-inbytesleft),(long)(sizeof(unsigned int)-outbytesleft));
  64.       exit(1);
  65.     }
  66.     return 1;
  67.   }
  68. }
  69.  
  70. int main (int argc, char* argv[])
  71. {
  72.   const char* charset;
  73.   iconv_t cd;
  74.  
  75.   if (argc != 2) {
  76.     fprintf(stderr,"Usage: table-to charset\n");
  77.     exit(1);
  78.   }
  79.   charset = argv[1];
  80.  
  81.   cd = iconv_open("UCS-4-INTERNAL",charset);
  82.   if (cd == (iconv_t)(-1)) {
  83.     perror("iconv_open");
  84.     exit(1);
  85.   }
  86.  
  87.   {
  88.     unsigned int out;
  89.     unsigned char buf[4];
  90.     unsigned int i0, i1, i2, i3;
  91.     int result;
  92.     for (i0 = 0; i0 < 0x100; i0++) {
  93.       buf[0] = i0;
  94.       result = try(cd,buf,1,&out);
  95.       if (result < 0) {
  96.       } else if (result > 0) {
  97.         printf("0x%02X\t0x%04X\n",i0,out);
  98.       } else {
  99.         for (i1 = 0; i1 < 0x100; i1++) {
  100.           buf[1] = i1;
  101.           result = try(cd,buf,2,&out);
  102.           if (result < 0) {
  103.           } else if (result > 0) {
  104.             printf("0x%02X%02X\t0x%04X\n",i0,i1,out);
  105.           } else {
  106.             for (i2 = 0; i2 < 0x100; i2++) {
  107.               buf[2] = i2;
  108.               result = try(cd,buf,3,&out);
  109.               if (result < 0) {
  110.               } else if (result > 0) {
  111.                 printf("0x%02X%02X%02X\t0x%04X\n",i0,i1,i2,out);
  112.               } else if (strcmp(charset,"UTF-8")) {
  113.                 for (i3 = 0; i3 < 0x100; i3++) {
  114.                   buf[3] = i3;
  115.                   result = try(cd,buf,4,&out);
  116.                   if (result < 0) {
  117.                   } else if (result > 0) {
  118.                     printf("0x%02X%02X%02X%02X\t0x%04X\n",i0,i1,i2,i3,out);
  119.                   } else {
  120.                     fprintf(stderr,"%s: incomplete byte sequence\n",hexbuf(buf,4));
  121.                     exit(1);
  122.                   }
  123.                 }
  124.               }
  125.             }
  126.           }
  127.         }
  128.       }
  129.     }
  130.   }
  131.  
  132.   if (iconv_close(cd) < 0) {
  133.     perror("iconv_close");
  134.     exit(1);
  135.   }
  136.  
  137.   if (ferror(stdin) || ferror(stdout)) {
  138.     fprintf(stderr,"I/O error\n");
  139.     exit(1);
  140.   }
  141.  
  142.   exit(0);
  143. }
  144.